package com.qf.webmaster.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.webmaster.entity.User;
import com.qf.webmaster.feign.SearchClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@RequestMapping("/webmaster")
public class WebMasterController {

    @Value("${version}")
    private String version;

    @GetMapping("/version")
    public String version() throws InterruptedException {
        Thread.sleep(2000);
        return version;
    }




    /* @Resource
    private EurekaClient eurekaClient;*/

    /*@Resource
    private RestTemplate restTemplate;*/

    @Resource
    private SearchClient searchClient;

    @GetMapping("/hello")
    public String hello(){
        /*//1. 通过Eureka获取到Search服务的地址信息
        InstanceInfo search = eurekaClient.getNextServerFromEureka("SEARCH", false);
        String ipAddr = search.getIPAddr();
        int port = search.getPort();
        //2. 通过RestTemplate调用即可.
        String result = restTemplate.getForObject("http://" + ipAddr + ":" + port + "/search/hello", String.class);*/

        /*// 引用Ribbon时调用
        String result = restTemplate.getForObject("http://SEARCH/search/hello", String.class);*/
        String result = searchClient.hello();
        //3. 返回
        return result;
    }


    @GetMapping("/find/{id}")
    @HystrixCommand(fallbackMethod = "findByIdFallback",
            commandProperties = {
                @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
                @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),          // 请求总数
                @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),    // open状态维持时间
                @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "90")         // 错误阈值 40%
    })
    public String findById(@PathVariable String id){
        if("1".equalsIgnoreCase(id)){
            int i = 1 / 0;
        }
        return searchClient.findById(id);
    }

    public String findByIdFallback(@PathVariable String id){
        return "凉凉~~~~~";
    }

    @GetMapping("/find")
    @HystrixCommand(
            fallbackMethod = "findByConditionFallback",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.strategy",value = "THREAD"),
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1"),
                    @HystrixProperty(name = "execution.timeout.enabled",value = "false")
            })
    public String findByCondition(String name,String age){
        System.out.println(Thread.currentThread().getName());   // hystrix
        String result = searchClient.findByCondition(name, age);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        int i = 1 / 0;  // by zero
        return result;
    }

    // findByCondition的降级方法
    public String findByConditionFallback(String name,String age){
        // 返回托底数据源
        return "服务爆炸了,请稍后再试!!";
    }

    @PostMapping("/save")
    public String save(User user){
        String result = searchClient.save(user);
        return result;
    }

}
